<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('biUserApplication:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="用户名称">
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入用户名称" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="load()">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <a-table :columns="columns" :data-source="dataList" :rowKey="(record) => record.id">
        <span slot="sex" slot-scope="text">
          <span v-if="text == 1"> 男 </span>
          <span v-if="text == 0"> 女 </span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a @click="otherApp(record.id)">三方应用</a>
          <a-divider type="vertical" />
        </span>
      </a-table>
    </a-card>
    <a-modal
      :width="1200"
      title="应用列表"
      :visible="visible"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <!-- <a-table :columns="Appcolumns" :data-source="userAppData" :rowKey="(record) => record.id">
        <span slot="action" slot-scope="text, record">
          <a  @click="otherApp(record)">三方应用</a>
          <a-divider type="vertical" />
        </span>
      </a-table> -->
      <div style="height: 400px; overflow: auto; margin-bottom: 20px">
        <!-- <a-row>
          <a-col style="float: left" v-for="(item, index) in userAppData" :key="index" :span="6">
            <div>
              <img style="margin-bottom: 20px; width: 250px" :src="item.imageUrl" alt="图片" />
              <br />
              <span>{{ item.name }}</span>

              <a-popconfirm
                v-if="hasPerm('biIndexType:isEnable')"
                placement="topRight"
                :title="item.isEnable == 'Y' ? '确认停用' : '确认启用'"
                @confirm="edit(item)"
              >
                <a-button style="float: right; margin-right: 35px" v-if="item.isEnable == 'Y'">启用</a-button>
                <a-button style="float: right; margin-right: 35px" type="primary" v-if="item.isEnable == 'N'"
                  >取消</a-button
                >
              </a-popconfirm>
            </div>
          </a-col>
        </a-row> -->
        <a-row>
          <a-col
            v-for="(item, index) in userAppData"
            :key="index"
            :span="8"
            :style="{ display: index < count ? 'block' : 'none', marginBottom: '30px', align: 'middle' }"
          >
            <div style="width: 250px">
              <img style="margin-bottom: 10px" :src="item.imageUrl" alt="图片" />
              <!-- <img style="width : 270px" :src="item.imageUrl" alt="图片" /> -->
            </div>

            <div style="width: 327px">
              <span style="float: left; margin-top: 5px">{{ item.name }}</span>
              <a-popconfirm
                v-if="hasPerm('biIndexType:isEnable')"
                placement="topRight"
                :title="item.isEnable == 'Y' ? '确认停用' : '确认启用'"
                @confirm="edit(item)"
                style="float: right"
              >
                <a-button type="primary" v-if="item.isEnable == 'Y'">已启用</a-button>
                <a-button v-if="item.isEnable == 'N'">已停用</a-button>
              </a-popconfirm>
            </div>
          </a-col>
        </a-row>
      </div>
    </a-modal>
  </div>
</template>
<script>
import { STable, XDown } from '@/components'
import {
  biUserApplicationPage,
  biUserApplicationDelete,
  biUserApplicationExport,
  biUserApplicationIsEnable,
  userList,
  listByUser
} from '@/api/modular/main/biuserapplication/biUserApplicationManage'
import { getImg } from '@/utils/img'
export default {
  components: {
    STable,

    XDown
  },
  computed: {
    count() {
      return this.expand ? 11 : 7
    }
  },
  data() {
    return {
      // 当前用户id
      uid: '',
      // 用户的应用列表
      userAppData: [],
      // 用户列表
      dataList: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},

      visible: false,
      confirmLoading: false,
      // 表头
      columns: [
        {
          title: '用户账号',
          align: 'center',
          dataIndex: 'account'
        },
        {
          title: '用户名称',
          align: 'center',
          dataIndex: 'name'
        },

        {
          title: '性别',
          align: 'center',
          dataIndex: 'sex',
          scopedSlots: { customRender: 'sex' }
        },

        {
          title: '邮箱',
          align: 'center',
          dataIndex: 'email'
        },
        {
          title: '手机号',
          align: 'center',
          dataIndex: 'tel'
        },
        {
          title: '备注',
          align: 'center',
          dataIndex: 'remark'
        }
      ],
      Appcolumns: [
        {
          title: '应用名称',
          align: 'center',
          dataIndex: 'name'
        }
      ],
      tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return biUserApplicationPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          }
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      }
    }
  },
  created() {
    if (this.hasPerm('biUserApplication:edit') || this.hasPerm('biUserApplication:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    this.load()
  },
  methods: {
    // 启用\停用应用
    edit(item) {
      console.info(item)
      biUserApplicationIsEnable(item).then((res) => {
        if (res.success) {
          this.$message.success('操作成功')
          this.otherApp(this.uid)
        } else {
          this.$message.error('操作失败' + res.message) //  + res.message
        }
      })
    },
    // 用户应用列表
    otherApp(id) {
      this.visible = true
      const _that = this
      this.uid = id
      listByUser(id).then(async (res) => {
        if (res.success) {
          // res.data.forEach(function (item, index) {
          //   // debugger
          //   _that.imgUrl(item)
          // })
          // const appData = res.data
          // for (const item of appData) {
          //   const response = await getImg({ url: item.imageUrl })
          //   const imgData = await this.getImgData(response)
          //   item.imageUrl = imgData
          //   _that.$forceUpdate()
          // }
          _that.userAppData = res.data
        }
      })
    },
    // 用户列表
    load() {
      const _that = this
      userList(this.queryParam).then((res) => {
        if (res.success) {
          _that.dataList = res.data
        }
      })
    },
    // // 获取图片
    // getImgData(response) {
    //   // 将后台的图片二进制流转为base64
    //   return (
    //     'data:image/png;base64,' +
    //     btoa(new Uint8Array(response).reduce((data, byte) => data + String.fromCharCode(byte), ''))
    //   )
    // },
    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ id: record.id }]
      this.biUserApplicationDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { id: d }
      })
      this.biUserApplicationDelete(paramIds)
    },
    biUserApplicationDelete(record) {
      biUserApplicationDelete(record).then((res) => {
        if (res.success) {
          this.$message.success('删除成功')
          this.$refs.table.clearRefreshSelected()
        } else {
          this.$message.error('删除失败') // + res.message
        }
      })
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    /**
     * 批量导出
     */
    batchExport() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { id: d }
      })
      biUserApplicationExport(paramIds).then((res) => {
        this.$refs.batchExport.downloadfile(res)
      })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    showModal() {
      this.visible = true
    },
    handleOk(e) {
      this.visible = false
    },
    handleCancel(e) {
      console.log('Clicked cancel button')
      this.visible = false
    }
  }
}
</script>
<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
